﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using System.Data;

namespace Data
{
    [DataContract]
    public class PlaylistConnector
    {
        #region Объектная модель
        static protected readonly Dictionary<Guid, PlaylistConnector> _allPlaylistConnectors = new Dictionary<Guid, PlaylistConnector>();

        static private readonly String _tableName = "[playlistconnector]";
        static public String TableName
        {
            get { return _tableName; }
        }

        static public PlaylistConnector GetById(Guid id)
        {
            return _allPlaylistConnectors[id];
        }

        static public List<PlaylistConnector> GetByPlaylist(Playlist list)
        {
            List<PlaylistConnector> tmp = new List<PlaylistConnector>();

            foreach (var connector in _allPlaylistConnectors)
            {
                if (((PlaylistConnector)connector.Value).Playlist == list)
                {
                    tmp.Add((PlaylistConnector)connector.Value);
                }
            }

            return tmp;
        }

        static public PlaylistConnector GetBySongAndPlaylist(Song song, Playlist list)
        {
            if (null != song && null != list)
            {
                foreach (var connector in _allPlaylistConnectors)
                {
                    if (((PlaylistConnector)connector.Value).Playlist == list && ((PlaylistConnector)connector.Value).Song == song)
                    {
                        return (PlaylistConnector)connector.Value;
                    }
                }
            }

            return null;
        }

        #endregion
        [DataMember]
        protected Guid _id;
        public Guid ID
        {
            get { return _id; }
        }

        [DataMember]
        private Guid _id_song;
        public Guid Id_song
        {
            get { return _id_song; }
        }
        public Song Song
        {
            get { return Song.GetById(_id_song); }
            set { _id_song = value.ID; }
        }

        [DataMember]
        private Guid _id_playlist;
        public Guid Id_playlist
        {
            get { return _id_playlist; }
        }
        public Playlist Playlist
        {
            get { return Playlist.GetById(_id_playlist); }
            set { _id_playlist = value.ID; }
        }

        public PlaylistConnector(SqlDataReader reader)
        {
            _id = new Guid((String)reader["id"]);
            _id_song = new Guid((String)reader["id_song"]);
            _id_playlist = new Guid((String)reader["id_playlist"]);
        }

        public PlaylistConnector(Song song, Playlist list)
        {
            _id = Guid.NewGuid();
            _id_song = song.ID;
            _id_playlist = list.ID;

            _allPlaylistConnectors.Add(this._id, this);
        }

        public DataTable Converted
        {
            get
            {
                DataSet tmp_ds = new DataSet("ds_" + PlaylistConnector.TableName);
                DataTable tmp_dt = new DataTable(PlaylistConnector.TableName);

                tmp_dt.Columns.Add("id");
                tmp_dt.Columns.Add("id_song");
                tmp_dt.Columns.Add("id_playlist");

                DataRow tmp_row = tmp_dt.Rows.Add();

                tmp_row["id"] = this._id;
                tmp_row["id_song"] = this._id_song;
                tmp_row["id_playlist"] = this._id_playlist;

                tmp_ds.Tables.Add(tmp_dt);

                return tmp_dt;
            }
        }

        public void Delete()
        {
            _id_song = Guid.Empty;
            _id_playlist = Guid.Empty;
        }

        static public bool AddToAllPlaylistConnectors(PlaylistConnector connector)
        {
            if (null != connector)
            {
                if (!_allPlaylistConnectors.Keys.Contains(connector.ID))
                {
                    _allPlaylistConnectors.Add(connector.ID, connector);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }


    }
}
